package net.janeluo.rs.module;

import javax.servlet.http.HttpServletRequest;

import net.janeluo.rs.bean.JsonReturnMessage;
import net.janeluo.rs.entity.RS_User;
import net.janeluo.rs.service.UserManagerService;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * 用户登陆模块
 * 
 * @author DLK)JaneLuo
 * @version 1.0.0
 */
@IocBean
@At("/")
public class LoginModule {
    
    @Inject
    private UserManagerService userManagerService;
    
    @At("/Login")
    @Ok("json")
    public JsonReturnMessage login(HttpServletRequest req, @Param("userid")String userid, @Param("password") String password){
        JsonReturnMessage mess = new JsonReturnMessage();
        RS_User user = userManagerService.findUserById(userid);
        if (user == null) {
            mess.setSuccess("0");
            mess.setMessage("用户不存在！");
        } else if (!password.equals(user.getPassWord())) {
            mess.setSuccess("0");
            mess.setMessage("密码不正确！");
        } else {
            mess.setSuccess("1");
            req.getSession().setAttribute("user", user);
        }
        
        return mess;
    }
    
    /**
     * 退出处理
     */
    @At("/Logout")
    @Ok("->:/InitUserIndex.nut")
    public void logout(HttpServletRequest req){
        req.getSession().removeAttribute("user");
    }
}
